package com.heima;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.Config.EsConfig;
import com.heima.Mapper.ArticleContentMapper;
import com.heima.Mapper.ArticleMapper;
import com.heima.Server.ArticleFreemaker;
import com.heima.model.Article.Pojo.ApArticle;
import com.heima.model.Article.Pojo.ApArticleContent;
import com.heima.model.Article.Pojo.ApArticleToES;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.IOException;
import java.util.List;

@SpringBootTest
public class test {
    @Autowired
    EsConfig esConfig;

    @Autowired
    ArticleFreemaker articleFreemaker;

    @Autowired

    ArticleMapper articleMapper;

    @Autowired
    ArticleContentMapper articleContentMapper;

    @Test
    public void add() throws IOException {
        QueryWrapper<ApArticle> wrapper=new QueryWrapper<>();
        List<ApArticle> apArticles = articleMapper.selectList(wrapper);
        BulkRequest bulkRequest=new BulkRequest();
        for (ApArticle apArticle:apArticles)
        {
            QueryWrapper<ApArticleContent> wrapper1=new QueryWrapper<>();
            wrapper1.eq("article_id",apArticle.getId());
            String content = articleContentMapper.selectOne(wrapper1).getContent();
            ApArticleToES article=new ApArticleToES();
            BeanUtils.copyProperties(apArticle,article);
            article.setContent(content);
            System.out.println(article);
            bulkRequest.add(new IndexRequest("hmttartilce").
                    id(apArticle.getId().toString()).
                    source(JSON.toJSONString(article), XContentType.JSON));
        }
        esConfig.esclient().bulk(bulkRequest,RequestOptions.DEFAULT);
    }

    @Test
    public void minio() throws IOException {
//        File file=new File(".\\hhh1");
//        file.createNewFile();
//        articleFreemaker.GetStaticArtcle(1742536791019241474L);
    }


}
